package com.biostime.qdingding.modules.qd.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Member;
import com.biostime.qdingding.modules.qd.service.MemberService;
import com.biostime.qdingding.modules.sys.entity.Area;
import com.biostime.qdingding.modules.sys.service.AreaService;

/**
 * 会员Controller
 * @author phil
 * @version 2015-11-06
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/member")
public class MemberController extends BaseController {

	@Autowired
	private MemberService memberService;
	@Autowired
	private AreaService areaService;
	
	@ModelAttribute
	public Member get(@RequestParam(required=false) String id) {
		Member entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = memberService.get(new Member(id));
		}
		if (entity == null){
			entity = new Member();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:member:view")
	@RequestMapping(value = {"list", ""})
	public String list(Member member, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Member> page = memberService.findPage(new Page<Member>(request, response), member); 
		model.addAttribute("page", page);
		return "modules/qd/memberList";
	}

	@RequiresPermissions("qd:member:view")
	@RequestMapping(value = "form")
	public String form(Member member, Model model) {
		Area province = new Area();
		province.setLayer(2);
		List<Area> provinceList = areaService.findList(province);
		
		List<Area> cityList = null;
		if(member.getProviceId()!=null){
			Area city = new Area();
			city.setParent(new Area(String.valueOf(member.getProviceId())));
			city.setLayer(3);
			cityList = areaService.findList(city);
		}
		
		List<Area> areaList = null;
		if(member.getCityId()!=null){
			Area area = new Area();
			area.setParent(new Area(String.valueOf(member.getCityId())));
			area.setLayer(4);
			areaList = areaService.findList(area);
		}
		
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("cityList", cityList);
		model.addAttribute("areaList", areaList);
		model.addAttribute("member", member);
		return "modules/qd/memberForm";
	}

	@RequiresPermissions(value={"qd:member:edit","qd:member:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Member member, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, member)){
			return form(member, model);
		}
		memberService.save(member);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/member/?repage";
	}
	
	@RequiresPermissions("qd:member:edit")
	@RequestMapping(value = "delete")
	public String delete(Member member, RedirectAttributes redirectAttributes) {
		memberService.delete(member);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/member/?repage";
	}

}